<?php

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Http\Repositories\Services\IIm;
use App\Http\Repositories\Services\IUser;
use App\Util\ResultsVo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Lang;

class LoginController extends Controller
{

    protected $userService;
    protected $imService;

    public function __construct(IUser $userService,IIm $imService)
    {
        $this->userService = $userService;
        $this->imService = $imService;
    }

    public function login(Request $request){
        $user = $this->userService->byId(intval($request->get('id',1)));
        $token = auth()->login($user);
        $data = $this->respondWithToken($token);
        return response()->json(new ResultsVo(['data'=>$data,'message'=>Lang::get('response.success')]));
    }

    public function me()
    {
        $data = auth()->user();
        return response()->json(new ResultsVo(['data'=>$data,'message'=>Lang::get('response.success')]));
    }

    public function refresh()
    {
        $token = auth()->refresh();
        $data = $this->respondWithToken($token);
        return response()->json(new ResultsVo(['data'=>$data,'message'=>Lang::get('response.success')]));
    }

    protected function respondWithToken($token)
    {
        $data = [
            'access_token' => $token,
            'token_type' => 'Bearer',
            'expires_in' => auth()->factory()->getTTL() * 60
        ];
        return $data;
    }

    public function logout()
    {
        auth()->logout();
        return response()->json(['message' => 'Successfully logged out']);
    }

}